package com.squirrel.michale;

import java.util.Arrays;

/**
 * @author guanhao 观浩
 * @version 1.0.0.0
 * @createTime 2023/2/23 12:11 PM
 * @company Michale Squirrel
 * @link
 * @description
 */
public class LeetCode1775 {

    public int minOperations(int[] nums1, int[] nums2) {
        Arrays.sort(nums1);
        Arrays.sort(nums2);

        int left = nums1.length-1;
        int right = 0;
        int opCount = 0;
        int sum1 = getSum(nums1);
        int sum2 = getSum(nums2);
        if (sum1 < sum2) {
            return minOperations(nums2, nums1);
        }
        int delta = sum1 - sum2;
        while (delta > 0) {
            if (left < 0 && right == nums2.length) {
                return -1;
            } else if (left < 0) {
                delta -= 6 - nums2[right];
                right++;
            } else if (right == nums2.length) {
                delta -= nums1[left] - 1;
                left--;
            }else if (nums1[left] - 1 >= 6 - nums2[right]) {
                delta -= nums1[left] - 1;
                left--;
            }else{
                delta -= 6 - nums2[right];
                right++;
            }
            opCount++;
        }
        return opCount;
    }


    public int minOperations3(int[] nums1, int[] nums2) {
        Arrays.sort(nums1);
        Arrays.sort(nums2);

        int left = nums1.length-1;
        int right = 0;

        int opCount = 0;
        int sum1 = getSum(nums1);
        int sum2 = getSum(nums2);

        if (sum1 < sum2) {
            return minOperations(nums2, nums1);
        }
        int delta = sum1 - sum2;
        while (delta > 0) {
            if (nums1[left] - 1 >= 6 - nums2[right]) {
                delta -= nums1[left] - 1;
                opCount++;
//                nums1[left] = 1;
                left--;
                if (left < 0) {
                    return -1;
                }
            }else{
                delta -= 6 - nums2[right];
//                nums2[right] = 6 - nums2[right];
                opCount++;
                right++;
                if (right > nums2.length - 1) {
                    return -1;
                }
            }
        }
        return opCount;
    }

    public int minOperations2(int[] nums1, int[] nums2) {
        int opCount = 0;
        int sum1 = getSum(nums1);
        int sum2 = getSum(nums2);

        int delta = sum2 - sum1;
        int realDelta = Math.abs(delta);
        opCount += realDelta / 5;
        if (realDelta % 5 > 0) {
            opCount++;
        }
        if (nums2.length == 1) {
            if (nums1.length > nums2.length*6) {
                return -1;
            }
        }

        if (nums1.length == 1) {
            if (nums2.length > nums2.length*6) {
                return -1;
            }
        }
        return opCount;
    }


    private int getSum(int[] nums) {
        int sum = 0;
        for (int i = 0; i < nums.length; i++) {

            sum += nums[i];
        }
        return sum;
    }

    public static void main(String[] args) {

        LeetCode1775 leetCode1775 = new LeetCode1775();


      int[]  nums1 = new int[]{3,3,2,4,2,6,2};
        int[] nums2 = new int[]{6, 2, 6, 6, 1, 1, 4, 6, 4, 6, 2, 5, 4, 2, 1};
        System.out.println(leetCode1775.minOperations(nums1, nums2));
//        29 / 65 个通过的测试用例
//                输出
//        7
//        预期结果
//        8
        int[] nums12 = new int[]{6,6};
        int[] nums22 = new int[]{1};

        System.out.println(leetCode1775.minOperations(nums12, nums22));


    }
}
